package com.qingding.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnChangeLisener;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.qingding.R;

import java.util.Date;

import com.qingding.model.Remark;
import com.qingding.model.Remind;

import static com.qingding.utils.Some_Public_Data.COMMENT;
import static com.qingding.utils.Some_Public_Data.REMIND_FREQUENCY_AND_TIME;
import static com.qingding.utils.Some_Public_Data.TYPE;

public class NewRemind extends AppCompatActivity implements OnChangeLisener, OnSureLisener {
    Remind remind;
    Remark remark;
    Intent intent;
    private TextView date_edit;//日期
    private EditText editing_title;//标题
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_remind);
        editing_title = (EditText) findViewById(R.id.editing_title);
        date_edit = (TextView) findViewById(R.id.date_edit);
        // 获取编辑框焦点
        editing_title.setFocusable(true);
        //打开软键盘
        InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
    public void finish(View view) {
        this.finish();
    }

    /**
     * 保存
     * @param view
     */
    public void saveRemind(View view){
        Remind remind = new Remind();
        remind.setTitle(editing_title.getText().toString());
        remind.setDateAndTime(date_edit.getText().toString());
        remind.save();
        finish();
    }

    /**
     * 选择日期
     * @param view
     */
    public void chooseDate(View view){
        /*TYPE_ALL–年、月、日、星期、时、分
        TYPE_YMDHM–年、月、日、时、分
        TYPE_YMDH–年、月、日、时
        TYPE_YMD–年、月、日
        TYPE_HM–时、分*/
        DatePickDialog dialog = new DatePickDialog(this);
        //设置上下年分限制
        dialog.setYearLimt(5);
        //设置标题
        dialog.setTitle("选择时间");
        //设置类型
        dialog.setType(DateType.TYPE_YMD);
        //设置消息体的显示格式，日期格式
        dialog.setMessageFormat("yyyy-MM-dd HH:mm");
        //设置选择回调
        dialog.setOnChangeLisener(this);
        //设置点击确定按钮回调
        dialog.setOnSureLisener(this);
        dialog.show();
    }

    public void chooseType(View view){
        intent=new Intent(this,Types.class);
        startActivityForResult(intent, TYPE);
    }
    public void editComment(View view){
        intent=new Intent(this,Comment.class);
        startActivityForResult(intent,COMMENT);
    }
    public void remindDate(View view){
        intent=new Intent(this,RemindTime.class);
        startActivityForResult(intent,REMIND_FREQUENCY_AND_TIME);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        remind=new Remind();
        switch (resultCode){
            case TYPE:
                remind.setType(data.getExtras().getString("type"));
                Toast.makeText(this,remind.getType(),Toast.LENGTH_SHORT).show();
                break;
            case COMMENT:
                remind.setComment(data.getExtras().getString("comment"));
                Toast.makeText(this,data.getExtras().getString("comment"),Toast.LENGTH_SHORT).show();
                break;
        }

    }

    @Override
    public void onChanged(Date date) {
    }

    /**
     * 日期回调
     * @param date
     */
    @Override
    public void onSure(Date date) {
        date_edit.setText(date.toString());
        Toast.makeText(this,date.toString(),Toast.LENGTH_SHORT).show();
    }
}
